global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_A12_shockvar_countryseries.log, replace name(fig)

capture noi {

    * Figure shockvar countryseries

    use ${final_dir}/regression_dataset_from1970_tfacit1_countrylevel_resid, clear
    keep if inlist(country, "US", "DE", "FR", "GB", "JP", "IT")

    * aesthetics
    qui include ${code_dir}/config/figuretools.do
    grstyle set margin ".25 2.3 .25 .25", cm: graph
    *next line specifies color on/off
    global color "cl"

    if "${color}" == "cl" {
        colorpalette scico bamO, n(6) saturate(40) intensity(0.9) nograph  // intensity(0.93) saturate(10) //  // sico Bold; scico roma
        local cpl_name "_cl"
        local color1 "`r(p1)'"
        local color2 "`r(p2)'"
        local color3 "`r(p3)'"
        local color4 "`r(p4)'"
        local color5 "`r(p5)'"
        local color6 "`r(p6)'"
    }

    * variable preparation
    local indep ISP
    local v _ssfiycy
    keep country year r`indep'`v'
    preserve

    *check coefs 5 years back
    * Set the data as time-series
    encode country, gen(country_numeric)
    tsset country_numeric year

    * Create a lagged variables for 5 years by country
    *don't know why this won't work with the loop. so I guess this will have to do
    gen lag_rISP_ssfiycy = l.rISP_ssfiycy
    gen lag2_rISP_ssfiycy = l.lag_rISP_ssfiycy
    gen lag3_rISP_ssfiycy = l.lag2_rISP_ssfiycy
    gen lag4_rISP_ssfiycy = l.lag3_rISP_ssfiycy
    gen lag5_rISP_ssfiycy = l.lag4_rISP_ssfiycy

    *regress on lags with country fixed effects

    reghdfe rISP_ssfiycy lag_rISP_ssfiycy
    local reg1_p1 = r(table)[4,1]

    reghdfe rISP_ssfiycy lag2_rISP_ssfiycy
    local reg1_p2 = r(table)[4,1]

    reghdfe rISP_ssfiycy lag3_rISP_ssfiycy
    local reg1_p3 = r(table)[4,1]

    reghdfe rISP_ssfiycy lag4_rISP_ssfiycy
    local reg1_p4 = r(table)[4,1]

    reghdfe rISP_ssfiycy lag5_rISP_ssfiycy
    local reg1_p5 = r(table)[4,1]
    di "`reg1_p1' `reg1_p2' `reg1_p3' `reg1_p4' `reg1_p5'"

    *and as expanding version
    reghdfe rISP_ssfiycy lag_rISP_ssfiycy
    local reg1_p1 = r(table)[4,1]
    reghdfe rISP_ssfiycy lag_rISP_ssfiycy lag2_rISP_ssfiycy
    local reg2_p1 = r(table)[4,1]
    local reg2_p2 = r(table)[4,2]
    reghdfe rISP_ssfiycy lag_rISP_ssfiycy lag2_rISP_ssfiycy lag3_rISP_ssfiycy
    local reg3_p1 = r(table)[4,1]
    local reg3_p2 = r(table)[4,2]
    local reg3_p3 = r(table)[4,3]
    reghdfe rISP_ssfiycy lag_rISP_ssfiycy lag2_rISP_ssfiycy lag3_rISP_ssfiycy lag4_rISP_ssfiycy
    local reg4_p1 = r(table)[4,1]
    local reg4_p2 = r(table)[4,2]
    local reg4_p3 = r(table)[4,3]
    local reg4_p4 = r(table)[4,4]
    reghdfe rISP_ssfiycy lag_rISP_ssfiycy lag2_rISP_ssfiycy lag3_rISP_ssfiycy lag4_rISP_ssfiycy lag5_rISP_ssfiycy
    local reg5_p1 = r(table)[4,1]
    local reg5_p2 = r(table)[4,2]
    local reg5_p3 = r(table)[4,3]
    local reg5_p4 = r(table)[4,4]
    local reg5_p5 = r(table)[4,5]

    di "`reg1_p1'"
    di "`reg2_p1' `reg2_p2'"
    di "`reg3_p1' `reg3_p2' `reg3_p3'"
    di "`reg4_p1' `reg4_p2' `reg4_p3' `reg4_p4'"
    di "`reg5_p1' `reg5_p2' `reg5_p3' `reg5_p4' `reg5_p5'"


    restore

    reshape wide r`indep'`v', i(year) j(country, string)
    ren r`indep'`v'* `indep'*
    list `indep'* if year == 2009



    if "`indep'" == "ISP" {
            local title "Inverse skill premium"
        } 
        if "`indep'" == "LSW" {
            local title "Log low-skill wage" 
        }
        if "`indep'" == "HSW" {
            local title "Log high-skill wage" 
        }

    * output graph
    twoway connected `indep'US `indep'DE `indep'JP `indep'FR `indep'GB `indep'IT year, ///
    xlabel(1996 (4) 2009) ///
    ylab(#5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) ///
    lpat(solid dot dash_dot dash shortdash longdash_dot) ///
    msym(O D T S X +) ///
    msize(small small small small small small) ///
    lw(1.3pt 1.3pt 1.3pt 1.3pt 1.3pt 1.3pt) ///
    ytitle("`title'" "(residualized)") yscale(titlegap(*-4)) ///
    xtitle("Year") ///
    legend(off) /// 
    color("`color1'" "`color2'" "`color3'" "`color4'" "`color5'" "`color6'") ///
    text(-.014 2007.3 "United States", color(`color1') place(e) just(left)) text(-.005 2007.8 "Germany", color(`color2') place(e) just(left)) ///
    text(-.00154 2009.3 "Japan", color(`color3') place(e) just(left)) text(.00997 2009.3 "France", color(`color4') place(e) just(left)) ///
    text(.00340 2009.3 "United" "Kingdom", color(`color5') place(e) just(left)) text(.02191 2009.3 "Italy", color(`color6') place(e) just(left)) ///
    name(r`indep'`v'`cpl_name', replace)
    graph export ${fig_dir}/appendix/Figure_A12_shockvar_`indep'`v'_countryseries`cpl_name'.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A12_shockvar_`indep'`v'_countryseries`cpl_name'.eps, replace
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig